//
//  JWKRefreshControl.swift
//  Weibo
//
//  Created by 姜维克 on 16/5/27.
//  Copyright © 2016年 姜维克. All rights reserved.
//

import UIKit

class JWKRefreshControl: UIControl {
    
    enum RefreshStatus {
        //下拉起飞--没有完全露出来
        case normal
        
        //准备起飞--准备要刷新
        case prepare
        
        //正在起飞--转动菊花正在刷新
        case fresh
    }

    //设置scrollView的属性
    var scrollView : UIScrollView?
    
    //设置刷新状态
    var refreshStatus : RefreshStatus = .normal{
        didSet{
            
            switch refreshStatus {
            case .normal:
                //普通状态
                tipLabel.text = "下拉起飞"
                
                //隐藏箭头
                arrowImageView.hidden = false
                
                //箭头回到初始位置
                
                UIView.animateWithDuration(0.25, animations: {
                    self.arrowImageView.transform = CGAffineTransformIdentity
                })
                
                //停止菊花
                
                indicatorView.stopAnimating()
                
                break
                
            case .prepare:
                //准备状态
                tipLabel.text = "准备起飞"
                
                //隐藏箭头
                arrowImageView.hidden = false
                
                //箭头转动180度
                UIView.animateWithDuration(0.25, animations: {
                    self.arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
                })
                
                
                //停止转动菊花
                indicatorView.stopAnimating()
                
                break
                
            case .fresh:
                //正在刷新状态
                tipLabel.text = "正在起飞"
                
                //隐藏箭头
                arrowImageView.hidden = true
                
                //转动菊花
                indicatorView.startAnimating()
                
                //给被监听者发送时间
                sendActionsForControlEvents(.ValueChanged)
                
                break
            
                
            }
            
            
            
        }
    }
    
 
    
    
    //重写willMoveToSuperview方法
    override func willMoveToSuperview(newSuperview: UIView?) {
        //如果新的父视图 是scrollView的话就添加KVO监听属性变化
        if let fatherView = newSuperview as? UIScrollView {
            self.scrollView = fatherView
            
            fatherView.addObserver(self, forKeyPath: "contentOffset", options: .New, context: nil)
        }
        
    }
    
    //MARK:-添加了KVO监听一定要移除
    deinit{
        scrollView?.removeObserver(self, forKeyPath: "contentOffset")
    }
    
    //MARK:重要的是实现键值监听方法
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
//        print(change)//打印的是父视图的偏移量
        
        let offsetY = scrollView?.contentOffset.y
        
        //先判断是否有被拖拽状态
        if scrollView!.dragging{
            //被拖拽 只有两种情况
            if offsetY > -124{
                refreshStatus = .normal
            }else{
                refreshStatus = .prepare
            }
            
            
        }else  {
            if offsetY <= -124{
                refreshStatus = .fresh
            }
        }
        
//        根据偏移量来设置控件不同的状态
//        if offsetY > -124{
//            //如果y方向的偏移量>-124,那就是没有完全露出
//            refreshStatus = .normal
//        }else if refreshStatus != .pulling {
//            //如果y方向的偏移量<=-124,那就是已经完全露出
//            refreshStatus = .refresh
//        }else{
//            refreshStatus = .pulling
//        }
    }
    
    
    //懒加载子控件
    //箭头
    lazy var arrowImageView : UIImageView = UIImageView(image: UIImage(named: "tableview_pull_refresh"))
    //提示文案
    lazy var tipLabel : UILabel = UILabel.init(title: "测试", textColor: UIColor.darkGrayColor(), font: 14)
    //小菊花
    lazy var indicatorView : UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
    
    
    
    
    override init(frame: CGRect) {
        
        let rect = CGRectMake(0, -60, UIScreenWidth, 60)
        super.init(frame: rect)
        
        backgroundColor = UIColor.orangeColor()
        //注册监听
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        addSubview(arrowImageView)
        addSubview(tipLabel)
        addSubview(indicatorView)
        
        arrowImageView.contentMode = .ScaleAspectFill
        //手动写代码需要自己切除,xib和sb中会自动切除
        arrowImageView.clipsToBounds = true
        
        arrowImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(standardMargin)
            make.height.equalTo(20)
        }
        
        tipLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(arrowImageView.snp_bottom).offset(standardMargin)
        }
        
        indicatorView.snp_makeConstraints { (make) in
            make.center.equalTo(arrowImageView)
        }
    }
    
    


}
